package state.state;

/**
 * @description: 状态机实体 内部封装了 糖果机的状态
 * 通过改变状态来调用状态里的方法 这样新增加状态只需添加该状态下的糖果机动作即可
　* @author 周杨
　* @date 2019/6/5  16:16
**/
public class CandyMachine {
    State soldOutState;
    State onReadyState;
    State hasCoin;
    State soldState;
    State winnerState;
    private State state;
    private int count = 0;
    public CandyMachine(int count){
        //实例化这几种状态
        this.count = count;
        soldOutState = new SoldOutState(this);
        onReadyState = new OnReadyState(this);
        hasCoin = new HasCoin(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);
        if (count > 0){
            state = onReadyState;
        }else {
            state = soldOutState;
        }
    }
    void setState(State state){
        this.state = state;
    }
    public void insertCoin(){
        state.insertCoin();
    }
    public void returnCoin(){
        state.returnCoin();
    }
    public void turnCrank(){
        state.turnCrank();
        state.dispense();
    }
    void releaseCandy(){
        if (count > 0){
            count = count - 1;
            System.out.println("A candy rolling out!");
        }
    }
    int getCount(){
        return count;
    }
    public void printState(){
        state.printState();
    }
}
